package com.geotmt.billingcenter.modules.params.mapper.provider;

import com.alibaba.druid.util.StringUtils;
import org.apache.ibatis.annotations.Param;

/**
 * @Description Param SQL
 * @Author      yanghanwei
 * @Mail        yanghanwei@geotmt.com
 * @Date        2019/3/20 20:54
 * @Version     v1
 **/
public class ParamProvider {

    /**
     * 产品List
     * @returnoutInterfaceList
     */
    public String productList(){
        StringBuffer sql = new StringBuffer();
        sql.append("SELECT id, product_code productCode, product_name productName,product_name name FROM t_product");

        return sql.toString();
    }

    /**
     * 客户List
     * @param productCode
     * @return
     */
    public String companyList(String productCode){
        StringBuffer sql = new StringBuffer();
        sql.append("SELECT t.id,t.company_simple_name companySimpleName,t.company_simple_name name, t.company_full_name ")
                .append(" FROM t_company_info t WHERE t.id IN (")
                .append(" SELECT distinct(com.id)")
                .append(" FROM t_company_info com")
                .append(" LEFT JOIN `t_customer_product` cupr ON com.id=cupr.`customer_id`")
                .append(" LEFT JOIN t_product prod ON prod.id=cupr.`product_id`");
        if(!StringUtils.isEmpty(productCode)){
            sql.append(" WHERE prod.product_code=#{productCode}");
        }
        sql.append(")");
        return sql.toString();
    }

    /**
     * 用户List
     * @param productCode
     * @param companyId
     * @return
     */
    public String userList(@Param(value = "productCode") String productCode,
                           @Param(value = "companyId") Integer companyId){
        StringBuffer sql = new StringBuffer();

        sql.append("SELECT user.id,user.account,user.real_name realName,user.real_name name FROM t_cuser_product tcp")
                .append(" LEFT JOIN t_product tp")
                .append(" ON tcp.`product_id` = tp.`id`")
                .append(" LEFT JOIN t_cuser_info user")
                .append(" ON tcp.`cuser_id` = user.id")
                .append(" WHERE 1=1 ");
        if(!StringUtils.isEmpty(productCode)){
            sql.append(" AND tp.product_code=#{productCode}");
        }
        if(null != companyId){
            sql.append(" AND user.company_id=#{companyId}");
        }
        return sql.toString();
    }

    /**
     * 内部接口List
     * @param productCode
     * @param companyId
     * @param userId
     * @return
     */
    public String innerIntegerList(@Param(value = "productCode") String productCode,
                                   @Param(value = "companyId") Integer companyId,
                                   @Param(value = "userId") Integer userId){
        StringBuffer sql = new StringBuffer();
        sql.append("SELECT ter.id, ter.interface_name interfaceName,ter.interface_name name")
                .append(" FROM t_interface ter")
                .append(" WHERE ter.id IN")
                .append("(SELECT DISTINCT ti.id")
                .append(" FROM t_cuser_interface tci")
                .append(" LEFT JOIN t_interface ti ON tci.`interface_id` = ti.`id`")
                .append(" LEFT JOIN t_cuser_info `user` ON tci.`cuser_id` = user.id")
                .append(" LEFT JOIN t_product tp ON tp.`id` = ti.`product_id`")
                .append(" WHERE 1 = 1");
        if(!StringUtils.isEmpty(productCode)){
            sql.append(" AND tp.product_code=#{productCode}");
        }
        if(null != companyId){
            sql.append(" AND user.company_id=#{companyId}");
        }
        if(null != userId){
            sql.append(" AND user.id=#{userId}");
        }
        sql.append(")");
        return sql.toString();
    }

    /**
     * 数据源List
     * @return
     */
    public String dsList(){
        StringBuffer sql = new StringBuffer();
        sql.append("SELECT id,`name`,ds_full_name dsFullName FROM t_ds_info");
        return sql.toString();
    }

    /**
     * 外部接口List
     * @param dsId
     * @return
     */
    public String outInterfaceList(Integer dsId){
        StringBuffer sql = new StringBuffer();
        sql.append("SELECT out_ter.id,out_ter.interface_name interfaceName,out_ter.interface_name name FROM t_out_interface out_ter WHERE out_ter.id IN(")
                .append(" SELECT DISTINCT(out_iter.id) FROM t_out_interface out_iter")
                .append(" LEFT JOIN t_io_ds iods ON out_iter.id=iods.`out_if_id`")
                .append(" LEFT JOIN t_ds_info ds ON iods.`ds_id`=ds.`id`");
        if(null != dsId){
            sql.append(" WHERE ds.id=#{dsId}");
        }
        sql.append(")");
        return sql.toString();
    }
}
